#define _CRT_SECURE_NO_WARNINGS
#include"Stack.h"


void STInit(Stack* s)
{
	assert(s);
	s->a = NULL;
	s->capacity = s->top = 0;
}

void STPush(Stack* s, STDataType x)
{
	assert(s);

	if (s->capacity == s->top)
	{
		s->capacity = s->capacity == 0 ? 4 : s->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(s->a, sizeof(STDataType) * s->capacity);
		if (tmp == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		s->a = tmp;
	}

	s->a[s->top] = x;
	s->top++;
}

bool STEmpty(Stack* s)
{
	assert(s);

	return s->top == 0;
}


void STPop(Stack* s)
{
	assert(s);
	assert(!STEmpty(s));

	s->top--;
}

STDataType STTop(Stack* s)
{
	assert(s);
	assert(!STEmpty(s));

	return s->a[s->top - 1];
}

void STDestroy(Stack* s)
{
	assert(s);
	
	free(s->a);
	s->a = NULL;
	s->capacity = s->top = 0;
}

int STSize(Stack* ps)
{
	assert(ps);

	return ps->top;
}